package com.ruoyi.web.core.logger;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
@Configuration
public class AppPortLogger implements ApplicationListener<ApplicationReadyEvent> {
    @Value("${server.servlet.context-path:/}")
    private String contextPath;

    @Override
    public void onApplicationEvent(@NonNull ApplicationReadyEvent event) {
        WebServerApplicationContext context =
                (WebServerApplicationContext)
                        event.getApplicationContext();

        int port = context.getWebServer().getPort();
        String host = "localhost";

        try {
            host = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }

        String path = contextPath.equals("/") ? "" : contextPath;

        String baseUrl = String.format("http://%s:%d%s", host, port, path);

        log.info("✅ Undertow服务器已启动，监听端口: {}", port);
        log.info("🚀 服务地址: {}", baseUrl);
        // 或 /swagger-ui/index.html，根据你使用的文档工具修改
        log.info("📘 Swagger 文档地址: {}/doc.html", baseUrl);
    }
}